<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>注册-管理平台</title>
    <!-- 样 式 文 件 -->
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/pages/login.css"/>
</head>
<style>
    .layui-form {
        width: 360px !important;
        /*margin-top: 80px !important;*/
    }

    .zyl_lofo_main {
        background-color: rgba(255, 255, 255, 0.9);
        padding: 10px 20px;
    }

    .layui-elem-field legend {
        margin-left: 0;
        padding: 0;
        width: 100%;
        text-align: center;
        align-content: center;
    }
</style>
<!-- 代 码 结 构 -->
<body background="/app/admin/admin/images/background.svg" style="background-size: cover;">

<form class="layui-form zyl_lofo_main">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>注册-管理平台</legend>
    </fieldset>
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input lay-verify="required" hover class="layui-input" type="text" name="username" value=""
                   placeholder="请输入用户名"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input lay-verify="required|pass" hover class="layui-input" type="password" name="password" value=""
                   placeholder="请输入密码"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认密码</label>
        <div class="layui-input-block">
            <input lay-verify="required" hover class="layui-input" type="password" name="password_confirm" value=""
                   placeholder="请再次输入密码" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">QQ</label>
        <div class="layui-input-block">
            <input lay-verify="required" hover class="layui-input" type="text" name="qq" value="" placeholder="请输入QQ">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">微信</label>
        <div class="layui-input-block">
            <input lay-verify="required" hover class="layui-input" type="text" name="weixin" value="" placeholder="请输入微信">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input lay-verify="required|phone" autocomplete="off" lay-reqtext="请输入手机号" lay-affix="clear" hover class="layui-input demo-phone" type="text" name="mobile" value="" placeholder="请输入手机号">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">验证码</label>
        <div class="layui-input-block">
            <div class="layui-col-md6">
                <input lay-verify="required" hover class="layui-input" type="text" name="code" value=""
                       placeholder="请输入验证码">
            </div>
            <div class="layui-col-md6">
                <button type="button" plain="" class="pear-btn pear-btn-primary" id="sendsms" lay-on="get-vercode"> 发送验证码</button>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">推广人</label>
        <div class="layui-input-block">
            <input hover class="layui-input" type="text" name="invitation" id="invitation"
                   value="<?=$invitation?>" placeholder="请输入推广人用户名" disabled>
            <input class="layui-input" type="hidden" name="link_id" id="link_id"
                   value="<?=$link_id?>">
        </div>
    </div>
    <div class="layui-form-item">
        <button type="submit" class="pear-btn pear-btn-primary login" lay-submit lay-filter="login">
            注 册
        </button>
        <div class="layui-form-item" style="text-align: right">

            <?php if ($link_id): ?>
            <small>
                <a href="/app/user?invitation=<?=$invitation?>&link_id=<?=$link_id?>">登录</a>
            </small>
            <?php else: ?>
            <small>
                <a href="/app/user?invitation=<?=$invitation?>">登录</a>
            </small>
            <?php endif; ?>

        </div>
    </div>
</form>

<script>
    var color = localStorage.getItem("theme-color-color");
    var second = localStorage.getItem("theme-color-second");
    if (!color || !second) {
        localStorage.setItem("theme-color-color", "#2d8cf0");
        localStorage.setItem("theme-color-second", "#ecf5ff");
    }
</script>
<!-- 资 源 引 入 -->
<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'button', 'popup', 'layer', 'theme', 'admin'], function () {

        var $ = layui.$, layer = layui.layer, form = layui.form;
        var util = layui.util;

        // 字段验证允许为空
        form.verify({
            phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
            email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
            url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
            number: [/(^$)|^\d+$/, '只能填写数字'],
            date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
            identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"],
            pass: function(value) {
                if (!/(.+){6,12}$/.test(value)) {
                    return '密码必须 6 到 12 位';
                }
            }
        });
        // 提 交
        form.on('submit(login)', function (data) {
            if (data.field.password != data.field.password_confirm) {
                layui.popup.failure('两次密码输入不一致');
                return false;
            }

            layer.load();
            $.ajax({
                url: '/app/admin/account/register_user',
                type: "POST",
                data: data.field,
                success: function (res) {
                    layer.closeAll('loading');
                    if (!res.code) {
                        layui.popup.success(res.msg, function () {
                            window.location.href = "/app/user";
                            // location.reload();
                        })
                    } else {
                        layui.popup.failure(res.msg)
                    }
                }
            });
            return false;
        });


        // let invitation = layui.$('#invitation').val();
        // if (invitation) {
        //     layui.$('#invitation').attr('disabled', true);
        // }

        // 普通事件
        util.on('lay-on', {
            // 发送验证码
            "get-vercode": function(othis){
                var isvalid = form.validate('.demo-phone'); // 主动触发验证，v2.7.0 新增
                // 验证通过
                if(isvalid){
                    // 此处可继续书写「发送验证码」等后续逻辑
                    var mobile = $("input[name='mobile']").val();
                    $.ajax({
                        url: "/app/admin/v1-sms/send",
                        type: "post",
                        dateType: "json",
                        data: {
                            event: 'register_user',
                            mobile: mobile,
                        },
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            // 发送成功
                            settime($('#sendsms'));
                            layui.popup.success('发送成功');
                        },
                        error: function () {
                            layui.popup.failure('网络错误');
                            return false;
                        }
                    })
                }
            }
        });

        //发送验证码倒计时
        var countdown = 60;

        function settime(obj) {
            if (countdown == 0) {
                //obj.removeattr("disabled");
                obj.css('background-color', '#1E9FFF')
                countdown = 60;
                obj.attr("disabled", false);
                obj.html('发送给验证码');
                return true;
            } else {
                obj.css('background-color', 'gray');
                obj.attr("disabled", true);
                obj.html(countdown + 's');
                countdown--;
            }
            setTimeout(function () {
                settime(obj);
            }, 1000)
        }

    })
</script>
</body>
</html>
